<?php
class stYoutubeToolboxstatistics_model extends stmodel_2_1{

    public function __construct($config) {
        parent::__construct ( $config );
        $this->setTableName($this->prefix.'st_youtube_statistic');
        $this->setPrimaryKey('id');
    }

    public function saveStstatistics($videoFeed) {
        $time = date('Y-m-d H:i:s', strtotime("-1 day"));
        $dataInsert = array();
        $i=0;
        $user_id = $videoFeed['user_id'];
        $videoFeed = $videoFeed['video_entry'];
        foreach ($videoFeed as $videoEntry) {
            $dataInsert[$i]['id'] = 'NULL';
            $dataInsert[$i]['user_id'] = $user_id;
            $dataInsert[$i]['cdate'] = $time;
            $dataInsert[$i]['video_id'] = $videoEntry->getVideoId();
            $dataInsert[$i]['view'] = $videoEntry->getVideoViewCount();
            $i++;
        }
        $this->inserRecords($dataInsert) ;
    }

    public function inserRecords($data = '', $where = ''){
        if(count($data)>0){
            $query = "INSERT INTO $this->_tableName
                    ( id, user_id, video_id, view, cdate)
                    VALUES
                    ";
            $query1 = array();
            foreach ($data as $value) {
                $query1[] = $this->prepare("(%d, %d, %s, %d, %s)",
                        $value['id'], $value['user_id'], $value['video_id'], $value['view'], $value['cdate']
                        );
            }
            $query1 = implode(', ', $query1);
            $query .= ' '.$query1;
//            echo $query;
            return $this->query( $query );
        }else{
            return false;
        }
    }

    public function getRecords($args = '') {
        $default_args = array(
            'fields' => '*',
            'and_where' => '',
            'or_where' => '',
            'where' => '',
            'order' => '',
            'limit' => '',
            'group_by' => '',
            'custom_query' => '',
        );
        if ($args == '')
            $args = $default_args;

        $fields = $args['fields'] ? $args['fields'] : '*';
        $and_where = $args['and_where'] ? $args['and_where'] : '';
        $or_where = $args['or_where'] ? $args['or_where'] : '';
        $custom_where = $args['where'] ? $args['where'] : '';
        $order = $args['order'] ? $args['order'] : '';
        $limit = $args['limit'] ? $args['limit'] : '';
        $group_by = $args['group_by'] ? $args['group_by'] : '';
        $custom_query = $args['custom_query'] ? $args['custom_query'] : '';
        if (!empty($custom_query)) {
            $query = $custom_query;
        } else {
            if ($fields != '*')
                $fields = implode(', ', $fields);
            //
            $where = ' WHERE ';

            if ($and_where != '')
                $where .= implode('', $and_where);
            if ($or_where != '')
                $where .= implode('', $or_where);
            if ($custom_where != '')
                $where .= $custom_where;
            if ($where == ' WHERE ')
                $where = '';
            //
            if ($order != '')
                $order = ' ORDER BY ' . implode(' ', $order);
            //
            if ($limit != '')
                $limit = ' LIMIT ' . implode(', ', $limit);
            if ($group_by != '')
                $group_by = ' GROUP BY ' . $group_by;
            //

            $table_video = $this->prefix.'st_youtubevideo';
            $table_user = $this->prefix.'st_youtube_user';
            $query = "SELECT $fields
            FROM $this->_tableName statistic
            LEFT JOIN $table_video video on video.video_id = statistic.video_id
            INNER JOIN $table_user user on user.id = statistic.user_id
            $where
            $group_by
            $order
            $limit
            ";
        }
//        echo $query.'<br/>  ';
        return $this->get_results($query);
    }

    public function getDate() {
        $query = "SELECT MONTH(cdate) month, YEAR(cdate) year, DAY(cdate) day
        FROM {$this->_tableName}
        ";
        return $this->get_results($query);
    }

    public function checkIsCronjobYet($day = '', $month = '', $year = '') {
        $query = "SELECT id
        FROM {$this->_tableName}
        WHERE DAY(cdate) = $day AND MONTH(cdate) = $month AND YEAR(cdate) = $year
        ";
        $result = $this->get_var($query);
        return $result;
    }
}